package com.apps.essentials;

import java.text.DecimalFormat;

public class ComputeDistance {
	
	
	public ComputeDistance(){
		
	}
	public double distanceFromList(int lat1, int long1, int lat2, int long2){
		
		double theta = (double)(long1/1E6) - (double)(long2/1E6);
		double distance = Math.sin(degreeToRad(lat1 / 1E6)) * Math.sin(degreeToRad(lat2 / 1E6))
				+ Math.cos(degreeToRad(lat1 / 1E6)) * Math.cos(degreeToRad(lat2 / 1E6))
				* Math.cos(degreeToRad(theta));
		distance = Math.acos(distance);
		distance = radToDegree(distance);
		distance = distance * 60;//nautical miles/degree
		distance = distance * 1852;//meters/nautical miles
		DecimalFormat df = new DecimalFormat("#.##");
		String cdistance = df.format(distance);
		distance = Double.parseDouble(cdistance);
		return distance;
	}
	
	private double degreeToRad(double deg){
		return (deg * Math.PI/180.0);
	}
	private double radToDegree(double rad){
		return (rad * 180 / Math.PI);
	}
}
